/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import pojo.Danhmuc;
import util.HibernateUtil;
import org.hibernate.classic.Session;
import org.hibernate.*;

/**
 *
 * @author PHT
 */
public class DanhMucDAO {

    public static List<Danhmuc> danhMucMenu;
    public static List<Danhmuc> danhMucTop;
    public static List<Danhmuc> dsDanhMuc;

    public static List<Danhmuc> layDanhMucMenu() {
        List<Danhmuc> danhMuc = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        String hql = "from Danhmuc dm where dm.laDanhMucCha = 1 order by dm.tenDanhMuc";
        Query query = session.createQuery(hql);
        query.setMaxResults(7);
        
        danhMuc = query.list();
        danhMucMenu = query.list();
        session.close();
        return danhMuc;
    }
    
    public static List<Danhmuc> layDanhMucCon(int maDanhMuc) {
        List<Danhmuc> danhMuc = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        String hql = "from Danhmuc dm where dm.danhmuc.maDanhMuc=:maDanhMuc";
        Query query = session.createQuery(hql);
        query.setInteger("maDanhMuc", maDanhMuc);
        danhMuc = query.list();
        danhMucMenu = query.list();
        session.close();
        return danhMuc;
    }

    public static List<Danhmuc> layDanhMucTop() {
        List<Danhmuc> danhMuc = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        String hql = "from Danhmuc dm where dm.laDanhMucTop = 1";
        Query query = session.createQuery(hql);
        query.setMaxResults(10);
        danhMuc = query.list();
        danhMucTop = query.list();
        session.close();
        return danhMuc;
    }
    
    public static List<Danhmuc> layDSDanhMuc() {
        List<Danhmuc> danhMuc = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        String hql = "from Danhmuc";
        Query query = session.createQuery(hql);
        danhMuc = query.list();
        dsDanhMuc = query.list();
        session.close();
        return danhMuc;
    }
    
    public static Danhmuc LayChiTietDanhMuc(int maDanhMuc) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Danhmuc dm = null;
        try {
            session.beginTransaction();
            dm = (Danhmuc) session.get(Danhmuc.class, maDanhMuc);
        } finally {
            session.close();
        }

        return dm;
    }
    
    public static List<Danhmuc> LayDanhMucChuaSP(){
        Session session = HibernateUtil.getSessionFactory().openSession();
        List<Danhmuc> listDM = null;
        try{
            
            String hql = "from Danhmuc dm where dm.laDanhMucCha = 0";
            Query query = session.createQuery(hql);
            listDM = query.list();
            
        }catch(Exception e){
            listDM = null;
        }finally{
            session.close();
        }
        
        return listDM;
    }
}
